home *** CD-ROM | disk | FTP | other *** search
/ Aminet 44 / Aminet 44 (2001)(GTI - Schatztruhe)[!][Aug 2001].iso / Aminet / dev / gui / gtlayout.lha / Source / LT_LabelWidth.c < prev    next >
C/C++ Source or Header  |  1999-01-02  |  3KB  |  155 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1999 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. #include "Assert.h"
  15.  
  16. /****** gtlayout.library/LT_LabelWidth ******************************************
  17. *
  18. *   NAME
  19. *    LT_LabelWidth -- Calculate the width of a string according
  20. *                     to the user interface font used.
  21. *
  22. *   SYNOPSIS
  23. *    Width = LT_LabelWidth(Handle,Label);
  24. *      D0                    A0    A1
  25. *
  26. *    LONG LT_LabelWidth(LayoutHandle *,STRPTR);
  27. *
  28. *   FUNCTION
  29. *    This routine calculates the width of strings in
  30. *    terms of pixels according to the user interface
  31. *    font associated with the LayoutHandle.
  32. *
  33. *    You can pass multi-line label texts to this routine,
  34. *    it will calculate the maximum length of the single
  35. *    lines. (V12)
  36. *
  37. *   INPUTS
  38. *    Handle - Pointer to LayoutHandle structure.
  39. *
  40. *   RESULT
  41. *    Width - Width of the text string in pixels.
  42. *
  43. ******************************************************************************
  44. *
  45. */
  46.  
  47. LONG LIBENT
  48. LT_LabelWidth(REG(a0) LayoutHandle *handle,REG(a1) STRPTR label)
  49. {
  50.     if(handle)
  51.     {
  52.         struct RastPort *rp = &handle->RPort;
  53.         LONG start;
  54.         LONG len;
  55.         LONG cnt;
  56.         LONG width,maxWidth;
  57.         LONG underscore;
  58.  
  59.         start = 0;
  60.         maxWidth = 0;
  61.         underscore = 0;
  62.  
  63.         cnt = 0;
  64.         len = 0;
  65.  
  66.         while(label[len])
  67.         {
  68.             if(label[len] == '_')
  69.             {
  70.                 if(!underscore)
  71.                     underscore = TextLength(rp,"_",1);
  72.  
  73.                 cnt += underscore;
  74.             }
  75.  
  76.             if(label[len] == '\n')
  77.             {
  78.                 if(len > start)
  79.                     width = TextLength(rp,&label[start],len - start) - cnt;
  80.                 else
  81.                     width = 0;
  82.  
  83.                 cnt = 0;
  84.                 start = len + 1;
  85.  
  86.                 if(width > maxWidth)
  87.                     maxWidth = width;
  88.             }
  89.  
  90.             len++;
  91.         }
  92.  
  93.         if(len > start)
  94.             width = TextLength(rp,&label[start],len - start) - cnt;
  95.         else
  96.             width = 0;
  97.  
  98.         if(width > maxWidth)
  99.             maxWidth = width;
  100.  
  101.         return(maxWidth);
  102.     }
  103.     else
  104.         return(0);
  105. }
  106.  
  107.  
  108. /*****************************************************************************/
  109.  
  110.  
  111. /****** gtlayout.library/LT_LabelChars ******************************************
  112. *
  113. *   NAME
  114. *    LT_LabelChars -- Calculate the width of a a string
  115. *                     according to the user interface font
  116. *                     associated with a LayoutHandle.
  117. *
  118. *   SYNOPSIS
  119. *    Length = LT_LabelChars(Handle,Label);
  120. *      D0                     A0    A1
  121. *
  122. *    LONG LT_LabelChars(struct LayoutHandle *,STRPTR);
  123. *
  124. *   FUNCTION
  125. *    Calculates the width of a string according to the user
  126. *    interface font used. The width is then converted to a number
  127. *    of characters suitable for using with the LA_Chars tag
  128. *    item when calling LT_New().
  129. *
  130. *    You can pass multi-line label texts to this routine,
  131. *    it will calculate the maximum length of the single
  132. *    lines. (V12)
  133. *
  134. *   INPUTS
  135. *    Handle - Pointer to LayoutHandle structure.
  136. *
  137. *    Label - Pointer to string.
  138. *
  139. *   RESULT
  140. *    Length - Number of characters that are considered
  141. *        equivalent to the string length in pixels.
  142. *
  143. ******************************************************************************
  144. *
  145. */
  146.  
  147. LONG LIBENT
  148. LT_LabelChars(REG(a0) LayoutHandle *handle,REG(a1) STRPTR label)
  149. {
  150.     if(handle)
  151.         return((LT_LabelWidth(handle,label) + handle->GlyphWidth - 1) / handle->GlyphWidth);
  152.     else
  153.         return(0);
  154. }
  155.